import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
import { ArticleService } from './article.service';
import { CreateArticleDto } from './dto/create-article.dto';
import { UpdateArticleDto } from './dto/update-article.dto';
import { AuthGuard } from '@nestjs/passport';

@Controller('article')
@UseGuards(AuthGuard('jwt'))
export class ArticleController {
    constructor(private readonly articleService: ArticleService) { }

    @Post()
    create(@Body() createArticleDto: CreateArticleDto) {
        return this.articleService.create(createArticleDto);
    }

    @Get()
    findAll(@Query() urlQuery: { title: string, status?: string, categoryId?: string, pageIndex: string, pageSize: string }) {
        const query = {
            title: urlQuery.title,
            status: urlQuery.status ? +urlQuery.status : undefined,
            categoryId: urlQuery.categoryId ? +urlQuery.categoryId : undefined,
            pageIndex: +urlQuery.pageIndex,
            pageSize: +urlQuery.pageSize
        }
        return this.articleService.findAll(query);
    }

    @Get(':id')
    findOne(@Param('id') id: string) {
        return this.articleService.findOne(+id);
    }

    @Patch(':id')
    update(@Param('id') id: string, @Body() updateArticleDto: UpdateArticleDto) {
        return this.articleService.update(+id, updateArticleDto);
    }

    @Delete(':id')
    remove(@Param('id') id: string) {
        return this.articleService.remove(+id);
    }
}
